package com.demo.ext.user;

import com.demo.common.model.User;
import com.demo.util.JdbcUtils;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.druid.DruidPlugin;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * @Author: linKe
 * @Date: 2022/4/11 15:19
 * @ClassName: UserActiveRecordTest
 * @Version: 1.0
 * @Description: 单ActiveRecord 测试类
 */

public class UserActiveRecordTest {

    @Before
    public void before() {
        DruidPlugin dp = JdbcUtils.getDruidPlugin();
        ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
        arp.addMapping("user", User.class);
        // 与 jfinal web 环境唯一的不同是要手动调用一次相关插件的start()方法
        dp.start();
        arp.start();
    }

    @Test
    public void findById() {
        // 通过上面简单的几行代码，即可立即开始使用
        new User().set("title", "title").set("content", "cxt text").save();
        User byId = new User().dao().findById(1);
        System.out.println(byId);
    }

    @Test
    public void deleteById() {
        // 删除id值为25的User
        boolean b = User.dao.deleteById(25);
        Assert.assertTrue(b);

    }

    @Test
    public void setSave(){
        // 创建name属性为James,age属性为25的User对象并添加到数据库
        boolean save = new User().set("content","测试添加数据").set("title","添加数据").set("name", "James").set("age", 25).save();
        Assert.assertTrue(save);

    }
    @Test
    public void findByIdSet(){
        // 查询id值为25的User将其name属性改为James并更新到数据库
        boolean update = User.dao.findById(4).set("name", "James").update();
        Assert.assertTrue(update);
    }
    @Test
    public void  findByIdLoadColumns(){
        // 查询id值为25的user, 且仅仅取name与age两个字段的值
        User user = User.dao.findByIdLoadColumns(4, "name, age");
        // 获取user的name属性
        String userName = user.getStr("name");
        System.out.println(userName);

        // 获取user的age属性
        Integer userAge = user.getInt("age");
        System.out.println(userAge);
    }
    @Test
    public void find(){
        // 查询所有年龄大于18岁的user
        List<User> users = User.dao.find("select * from user where age>18");
        users.forEach(System.out::println);
    }
    @Test
    public void findPage(){

        // 分页查询年龄大于18的user,当前页号为1,每页10个user
        Page<User> userPage = User.dao.paginate(1, 10, "select *", "from user where age > ?", 18);
        System.out.println(userPage);
    }


    void test() {










    }

}
